<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category AdminNav.php
 * @Depiction:
 * Author: hwy
 * Date: 2020/5/28  17:09
 * +--------------------------------------------------------+
 */

namespace App\Models;

use Auth;
use DB;
use app\Lib\Org\Data;

class AdminNav extends BaseModel
{
    public function getTreeData($type='tree',$order='')
    {
        //获取当前登录的用户
        $user = Auth::guard('admin')->user();

        if($user->id != 1){
            $permissions = $user->getAllPermissions();
            $permis = [];
            foreach($permissions as $v){
                $permis[] = $v->id;
            }
        }

            // 判断是否需要排序
            if (empty($order)) {
                // $data=AdminNav::query()->remember(60 * 60 * 24)->get()->toArray();
                $data=AdminNav::query()->get()->toArray();
            }else{
                if($user->id != 1){
                    $data=AdminNav::query()
                        ->whereIn('id',$permis)
                        ->orderBy($order)
                        ->get()
                        ->toArray();
                    // $data=AdminNav::query()
                    //     ->remember(60 * 60 * 24)
                    //     ->whereIn('id',$permis)
                    //     ->orderBy($order)
                    //     ->get()
                    //     ->toArray();
                }else{
                    // $data=AdminNav::query()
                    //     ->remember(60 * 60 * 24)
                    //     ->orderBy($order)
                    //     ->get()
                    //     ->toArray();
                    $data=AdminNav::query()
                        ->orderBy($order)
                        ->get()
                        ->toArray();

                }
            }


            // 获取树形或者结构数据
            if($type=='tree'){
                $data=Data::tree($data,'name','id','pid');
            }elseif($type="level") {
                $data = Data::channelLevel($data, 0, '&nbsp;', 'id');
            }

            return $data;
    }
}
